using MeiluluSqlite.DataMode;
using Microsoft.AspNetCore.Mvc;
using netcoreTextMvc.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace netcoreTextMvc.Controllers
{
    public class Money3Controller : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public string AddMoney3(string id, string type, string  number, string remarks, string date)
        {
            User user;
            var list = Program.sqlite.Select<User>("username='" + id + "'");
            if (list.Count > 1)
            {
                return "数据异常";
            }
            else if (list.Count == 0)
            {
                user = new User()
                {
                    creatTime = DateTime.Now,
                    password = "",
                    userid = Guid.NewGuid().ToString(),
                    username = id
                };
                Program.sqlite.Insert(user);
            }
            else
            {
                user = list[0];
            }
            if (!DateTime.TryParse(date, out DateTime crdate))
            {
                crdate = DateTime.Now;
            }
            MyMoney money = new()
            {
                changeTime = crdate,
                id = Guid.NewGuid().ToString(),
                userid = user.userid,
                remarks = remarks,
                type = Convert.ToInt32(type),
                value = Convert.ToDouble(number)
            };
            if (Program.sqlite.Insert(money))
            {
                return "添加完成";
            }
            else
            {
                return "添加失败";
            }
        }
        [HttpPost]
        public string SelectMoney3(string id,string selecttype)
        {
            List<MyMoney> myMoney = new();
            var list = Program.sqlite.Select<User>("username='" + id + "'");
            if (list.Count > 0)
            {
                if (string.IsNullOrEmpty(selecttype))
                {
                    myMoney = Program.sqlite.Select<MyMoney>("userid='" + list[0].userid + "' order by changeTime");
                }
                else
                {
                    myMoney = Program.sqlite.Select<MyMoney>("userid='" + list[0].userid + "' and remarks='" + selecttype + "' order by changeTime");
                }
            }
            myMoney.Sort((x, y) => y.changeTime.CompareTo(x.changeTime));//升序
            return JsonConvert.SerializeObject(myMoney);
        }
        [HttpPost]
        public string SelectMoney33(string id)
        {
            SelectData selectData = JsonConvert.DeserializeObject<SelectData>(id);
            List<MyMoney> myMoney = new();
            var list = Program.sqlite.Select<User>("username='" + selectData.id + "'");
            if (list.Count > 0)
            {
                if (string.IsNullOrEmpty(selectData.selecttype))
                {
                    myMoney = Program.sqlite.Select<MyMoney>("userid='" + list[0].userid + "' order by changeTime");
                }
                else
                {
                    myMoney = Program.sqlite.Select<MyMoney>("userid='" + list[0].userid + "' and remarks='" + selectData.selecttype + "' order by changeTime");
                }
            }
            myMoney.Sort((x, y) => y.changeTime.CompareTo(x.changeTime));//升序
            return JsonConvert.SerializeObject(myMoney);
        }
        [HttpPost]
        public string Deldata(string id)
        {
            if (Program.sqlite.Delate<MyMoney>(new MyMoney() { id = id }))
            {
                return "删除完成";
            }
            else
            {
                return "删除失败";
            }
        }
    }
}
